#include "StdAfx.h"
#include "MsgHook.h"
#include "Resource.h"

typedef UINT (*RegisterMsgHook)(HWND hwnd);
typedef void (*UnregisterMsgHook)();

MsgHook::MsgHook(void)
{
	mDllInstance = 0;
	mListener = 0;
}

MsgHook::~MsgHook(void)
{
}


void MsgHook::MsgProc( WPARAM wParam,LPARAM lParam )
{
	LogDebug(FMTVAR(wParam) + FMTVAR(lParam));
	if (mListener)
	{
		mListener->OnMsg(wParam, lParam);
	}
}

bool MsgHook::Start()
{
	mDllInstance = ::LoadLibrary(_T("FastHook.dll"));
	if (!mDllInstance)
	{
		return false;
	}

	RegisterMsgHook func = (RegisterMsgHook)GetProcAddress(mDllInstance, ("RegisterMsgHook"));
	if (func)
	{
		if(func(AfxGetMainWnd()->GetSafeHwnd()) != 0)
		{
			
			LogError("reg hook failed");
		}
	}
	

	return true;
}

void MsgHook::Stop()
{
	if (!mDllInstance)
	{
		return ;
	}

	UnregisterMsgHook func = (UnregisterMsgHook)GetProcAddress(mDllInstance, ("UnregisterMsgHook"));
	if (func)
	{
		func();
	}
	FreeLibrary(mDllInstance);
}